/****************************************************************************
 * file  : systask.c
 * creat : apleilx
 * data  : 2017/11/05
 *****************************************************************************/
#include "pincfg.h"

#define _UDISK_TASK_MODULE_
#include "udisk_task.h"
#undef _UDISK_TASK_MODULE_

#include "fx_api.h"
#include "os_obj.h"
#include "common.h"
#include "sys_init.h"
#include "com_obj.h"

void file_tst(void);
void spdf_task(char en);

VOID fx_udisk_driver(FX_MEDIA *media_ptr);

FX_MEDIA usb_disk;
FX_FILE usb_file;

#define FILE_W_TST_EN 1
#define FILE_R_TST_EN 0

/*****************************************************************************
 *
 * @brief   file test.
 *
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
void udisk_fx_file_tst(void)
{
	ULONG actual, index, cnt;
	uint32_t out_val, len;
	CHAR *local_buffer = malloc(10 * 1024);
	unsigned long *usb_media_mem = malloc(4 * 1024);
	fx_system_initialize();

	// open spim disk
	udisk_st.fil_opt_st =
		fx_media_open(
			&usb_disk,
			"USB DISK",
			fx_udisk_driver,
			0,
			usb_media_mem,
			4 * 1024);

	do
	{
		if (udisk_st.fil_opt_st != FX_SUCCESS)
		{
			break;
		}

		/* Open the test file.  */
		udisk_st.fil_opt_st = fx_file_open(&usb_disk, &usb_file, "main.c", FX_OPEN_FOR_READ);
		if (udisk_st.fil_opt_st == FX_SUCCESS)
		{
			udisk_st.fil_opt_st = fx_file_read(&usb_file, local_buffer, 60, &actual);
			if (udisk_st.fil_opt_st != FX_SUCCESS)
			{
				dbg_print("main.c open: FAIL!\n");
			}

			udisk_st.fil_opt_st = fx_file_close(&usb_file);
			if (udisk_st.fil_opt_st != FX_SUCCESS)
			{
				dbg_print("main.c close: FAIL!\n");
			}
		}

		/* Create a file called TEST.TXT in the root directory.  */
		udisk_st.fil_opt_st = fx_file_create(&usb_disk, "FILEX.CSV");

		/* Check the create udisk_st.fil_opt_st.  */
		if (udisk_st.fil_opt_st != FX_SUCCESS)
		{
			/* Check for an already created udisk_st.fil_opt_st. This is expected on the
			   second pass of this loop!  */
			if (udisk_st.fil_opt_st != FX_ALREADY_CREATED)
			{
				/* Create error, break the loop.  */
				dbg_print("FILEX.CSV creat: ERROR\n");
				break;
			}
			else
			{
				dbg_print("FILEX.CSV creat: FX_ALREADY_CREATED\n");
			}
		}
		else
		{
			dbg_print("FILEX.CSV creat: OK\n");
		}

		/* Open the test file.  */
		udisk_st.fil_opt_st = fx_file_open(&usb_disk, &usb_file, "FILEX.CSV", FX_OPEN_FOR_WRITE);
		if (udisk_st.fil_opt_st != FX_SUCCESS)
		{
			break;
		}

		/* point to end of the file.  */
		udisk_st.fil_opt_st = fx_file_seek(&usb_file, usb_file.fx_file_current_file_size);
		if (udisk_st.fil_opt_st != FX_SUCCESS)
		{
			/* Error performing file seek, break the loop.  */
			dbg_print("FILEX.CSV SEEK: ERROR\n");
			break;
		}

		/* Write a string to the test file.  */
#if FILE_W_TST_EN > 0
		out_val = 0;

		udisk_st.fil_write_time = app_tick_get();

		for (index = 0; index < 4; index++)
		{
			len = 0;

			for (cnt = 0; cnt < 128; cnt++)
			{
				i32_to_string(&local_buffer[len], out_val++, 0x63, 0x10);
				len += 10;
				local_buffer[len++] = ',';
				local_buffer[len++] = ' ';

				if (0x0F == (cnt & 0x0F))
				{
					local_buffer[len++] = '<';
					local_buffer[len++] = '>';
					local_buffer[len++] = '\r';
					local_buffer[len++] = '\n';
				}
			}
			udisk_st.fil_opt_st = fx_file_write(&usb_file, local_buffer, len);
		}

		udisk_st.fil_write_time = app_tick_get() - udisk_st.fil_write_time;
		udisk_st.file_write_size = index * (cnt * 12 + 2);

		/* Check the file write udisk_st.fil_opt_st.  */
		if (udisk_st.fil_opt_st != FX_SUCCESS)
		{
			dbg_print("SPIM.TXT WRITE: ERROR\n");
			/* Error writing to a file, break the loop.  */
			break;
		}
#endif

		/* Seek to the beginning of the test file.  */
		udisk_st.fil_opt_st = fx_file_seek(&usb_file, 0);

		/* Check the file seek udisk_st.fil_opt_st.  */
		if (udisk_st.fil_opt_st != FX_SUCCESS)
		{
			dbg_print("SPIM.TXT SEEK AFTER WRITE: ERROR\n");
			/* Error performing file seek, break the loop.  */
			break;
		}

#if FILE_R_TST_EN > 0
		/* Read the first 28 bytes of the test file.  */
		dbg_print("SPIM.TXT READ DAT :\n");
		while (1)
		{
			udisk_st.fil_opt_st = fx_file_read(&usb_file, local_buffer, 60, &actual);
			local_buffer[actual] = 0;

			/* Check the file read udisk_st.fil_opt_st.  */
			if (udisk_st.fil_opt_st != FX_SUCCESS)
			{
				if (udisk_st.fil_opt_st != FX_END_OF_FILE)
				{
					dbg_print("SPIM.TXT READ: ERROR -%d\n", udisk_st.fil_opt_st);
					/* Error reading file, break the loop.  */
					break;
				}
				else
				{
					dbg_print("%s", local_buffer);
					break;
				}
			}

			dbg_print("%s", local_buffer);

			osDelay(ms_ticks(10));
		}
		dbg_print("\n\nSPIM FILE READ END!\n\n");

		/* Check the file read udisk_st.fil_opt_st.  */
		if ((udisk_st.fil_opt_st != FX_SUCCESS) && (udisk_st.fil_opt_st != FX_END_OF_FILE))
		{
			break;
		}
#endif

		/* Close the test file.  */
		udisk_st.fil_opt_st = fx_file_close(&usb_file);

		/* Check the file close udisk_st.fil_opt_st.  */
		if (udisk_st.fil_opt_st != FX_SUCCESS)
		{
			/* Error closing the file, break the loop.  */
			break;
		}

		spdf_task(1);

	} while (0);

	fx_media_close(&usb_disk);

	free(local_buffer);
	free(usb_media_mem);
}
